/*
 *
 *  * | Licensed 未经许可不能去掉「OPENIITA」相关版权
 *  * +----------------------------------------------------------------------
 *  * | Author: xw2sy@163.com
 *  * +----------------------------------------------------------------------
 *
 *  Copyright [2024] [OPENIITA]
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * /
 */

package cc.iotkit.manager.service.impl;

import cc.iotkit.common.api.PageRequest;
import cc.iotkit.common.api.Paging;
import cc.iotkit.common.exception.BizException;
import cc.iotkit.common.satoken.utils.LoginHelper;
import cc.iotkit.common.utils.MapstructUtils;
import cc.iotkit.data.manager.IBizWorkorderReplace;
import cc.iotkit.manager.dto.bo.biz.BizWorkorderReplaceBo;
import cc.iotkit.manager.dto.bo.device.DeviceInfoPartsBo;
import cc.iotkit.manager.dto.vo.biz.BizWorkorderReplaceVo;
import cc.iotkit.manager.dto.vo.deviceinfo.DeviceInfoPartsVo;
import cc.iotkit.manager.service.IBizWorkorderReplaceService;
import cc.iotkit.model.biz.BizWorkorderReplace;
import cc.iotkit.model.device.DeviceInfoParts;
import cn.hutool.core.collection.CollectionUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 租户Service业务层处理
 *
 * @author Michelle.Chung
 */
@RequiredArgsConstructor
@Service
public class BizWorkorderReplaceServiceImpl implements IBizWorkorderReplaceService {

    private final IBizWorkorderReplace sysBizWorkorderReplaceData;

    public void deleteByPid(Collection<Long> pid){
        sysBizWorkorderReplaceData.deleteByPid(pid);
    }

    public BizWorkorderReplaceVo queryByPid(Long id) {
        return MapstructUtils.convert(sysBizWorkorderReplaceData.findByPid(id),BizWorkorderReplaceVo.class);
    }

    public BizWorkorderReplaceVo queryById(Long id) {
        return MapstructUtils.convert(sysBizWorkorderReplaceData.findById(id),BizWorkorderReplaceVo.class);
    }

    /**
     * 新增保存角色信息
     *
     * @param data 角色信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(BizWorkorderReplaceBo data) {

        sysBizWorkorderReplaceData.save(data.to(BizWorkorderReplace.class));
        return true;
    }

    @Transactional
    public List<BizWorkorderReplaceVo> saveAll(List<BizWorkorderReplace> list,Long pid){
        //获取原始数据
        BizWorkorderReplace cond=new BizWorkorderReplace();
        cond.setPid(pid);
        List<BizWorkorderReplace> oldList=sysBizWorkorderReplaceData.findAllByCondition(cond);

        //去除差异数据
        List<Long> oldIds=oldList.stream().map(item->item.getId()).collect(Collectors.toList());
        List<Long> newIds=list.stream().filter(m->m.getId()!=null).map(item->item.getId()).collect(Collectors.toList());
        Collection<Long> delIds = CollectionUtil.subtract(oldIds, newIds).stream().collect(Collectors.toList());
        sysBizWorkorderReplaceData.deleteByIds(delIds);

        //保存数据
        list.forEach(m->m.setPid(pid));
        List<BizWorkorderReplaceVo> results=  MapstructUtils.copyListToList(sysBizWorkorderReplaceData.saveAll(list),BizWorkorderReplaceVo.class);
        return results;
    }

    @Override
    public boolean deleteById(Long id) {
        sysBizWorkorderReplaceData.deleteById(id);
        return true;
    }

    @Transactional
    public boolean deleteByIds(Collection<Long> ids) {
        sysBizWorkorderReplaceData.deleteByIds(ids);
        return true;
    }



    @Override
    public Paging<BizWorkorderReplaceVo> queryPageList(PageRequest<BizWorkorderReplaceBo> request) {
        Paging<BizWorkorderReplace> pages=sysBizWorkorderReplaceData.findAllByConditions(request.to(BizWorkorderReplace.class));
        return pages.to(BizWorkorderReplaceVo.class);
    }

    @Override
    public List<BizWorkorderReplaceVo> queryList(BizWorkorderReplaceBo bo) {
        List<BizWorkorderReplace> list=sysBizWorkorderReplaceData.findAllByCondition(bo.to(BizWorkorderReplace.class));
        return MapstructUtils.copyListToList(list,BizWorkorderReplaceVo.class);
    }



}
